<?php

namespace StudyBuddy\Controllers;

use StudyBuddy\WebPage;
use StudyBuddy\Responder;

/**
 * Controller for getting array
 * of tags that partially match the 
 * value of 'q' using "starts with" regex search
 * 
 * This controller is used from the auto-complete tag widget
 * on the "Ask" form
 *
 */
class Taghint extends Titlehint
{

	protected function getData(){

		$q = $this->oRequest->get('q', 's');
		$q = mb_strtolower($q);
		d('looking for tag hint $q: '.$q);
		
		try{
			$cur = $this->oRegistry->Mongo->QUESTION_TAGS->find(
			array('tag' => array('$regex' => '^'.$q) ), array('tag'))
			->sort(array('i_count' => -1))
			->limit(200);
			$this->aData = iterator_to_array($cur, false);
			d('$this->aData: '.print_r($this->aData, 1));
		} catch(\MongoException $e){
			d('MongoException: '.$e->getMessage().' $q was: '.$q);
		}
		
		return $this;
	}
}
